$(function () {
    $("#jqGrid").jqGrid({
        url: '../codegentable/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true },		
			{ label: '表名', name: 'tableName', index: 'table_name', width: 80 }, 			
			{ label: '表类型', name: 'tableType', index: 'table_type', width: 80 ,
				formatter: function(value, options, row){
					if(value=="0"){
						return "单表";
					}else if(value=="1"){
						return "多表";
					}
				}						
			},
			{ label: '备注信息', name: 'remarks', index: 'remarks', width: 80 }, 					
			{ label: '是否同步数据库', name: 'syncDatabase', index: 'sync_database', width: 80 },
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        ondblClickRow : function(rowid,iRow,iCol,e){
        	window.location.href="../codegentable/edit?id="+rowid; 
        },
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		codegenTable: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.codegenTable = {};
			layer.open({
			    type: 1,  
			    area: ["800px","620px"],
			    title: '新增',
		        maxmin: true, //开启最大化最小化按钮
		        content: $("#winContent") ,
			    btn: ['确定', '关闭'],
			    yes: function(index, layero){
			    	var tableNames = getSelectedRowsById('tableJqGrid');
					var url = "../codegentable/code";
					var param = {};
					param.tables = JSON.stringify(tableNames);
					$.ajax({
						type: "POST",
					    url: url,
					    data: param,
					    success: function(r){
					    	layer.closeAll();
					    	if(r.code === 0){
								alert('操作成功', function(index){
									vm.reload();
								});
							}else{
								alert(r.msg);
							}
						}
					});
//			    	vm.reload();
				},
				cancel: function(index){ 
				}
			});
			
		    $("#tableJqGrid").jqGrid({
		        url: '../sys/generator/list',
		        datatype: "json",
		        colModel: [			
					{ label: '表名', name: 'tableName', width: 100, key: true },
					{ label: 'Engine', name: 'engine', width: 70},
					{ label: '表备注', name: 'tableComment', width: 100 },
					{ label: '创建时间', name: 'createTime', width: 100 }
		        ],
				viewrecords: true,
		        height: 385,
		        rowNum: 10,
				rowList : [10,30,50],
		        rownumbers: true, 
		        rownumWidth: 25, 
		        autowidth:true,
		        multiselect: true,
		        pager: "#tableJqGridPager",
		        jsonReader : {
		            root: "page.list",
		            page: "page.currPage",
		            total: "page.totalPage",
		            records: "page.totalCount"
		        },
		        prmNames : {
		            page:"page", 
		            rows:"limit", 
		            order: "order"
		        },
		        gridComplete:function(){
		        	//隐藏grid底部滚动条
		        	$("#tableJqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
		        }
		    });
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
            
            updateDialog("修改","winContent","jqGrid","800px","600px");
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.codegenTable.id == null ? "../codegentable/save" : "../codegentable/update";
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.codegenTable),
			    success: function(r){
			    	layer.closeAll();
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../codegentable/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../codegentable/info/"+id, function(r){
                vm.codegenTable = r.codegenTable;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});

